// pages/doctor-case-add-4/doctor-case-add-4.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBase: app.globalData.imgUrl,          // 线上图片
    localImg: app.globalData.staticimgUrl,  // 本地图片
    images: [],
    imagesurl: [], // 上传成功的图片地址
    content: '', // 详细信息
    oneType: '',
    twoType: '',
    threeType: '',
    goodsId: '',
    exampleId: '' // 案例id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.choosedid) {
      let tabs = options.choosedid.split(",");
      this.setData({
        oneType: tabs[0],
        twoType: tabs[1],
        threeType: tabs[2]
      });
    }
    this.setData({
      goodsId: options.goodsId || '',
      exampleId: options.exampleId || ''
    });
    if (options.exampleId) {
      this.getCaseDetail();
    }
  },

  getCaseDetail: function() {
    const that = this;
    const imgBase = app.globalData.imgUrl; // 线上图片
    app.request({
      url: 'doctor/docExample/detail',
      data: {
        exampleId: this.data.exampleId
      },
      success: function(res) {
        let imageArray = res.data[0]['exampleImgs'];
        let images = [],
          imagesUrl = [];
        imageArray.map(function(image, index) {
          images.push(imgBase + image.imgPath);
          imagesUrl.push(image.imgPath)
        });
        that.setData({
          content: res.data[0]['content'],
          images: images,
          imagesurl: imagesUrl,
          goodsId: res.data[0]['goodsId'],
          oneType: res.data[0]['oneTypeId'],
          twoType: res.data[0]['twoTypeId'],
          threeType: res.data[0]['threeTypeId']
        });
      }
    })
  },
  // 添加补充信息
  addContent: function(e) {
    var content = e.detail.value;
    this.setData({
      'content': content,
    });
  },

  // 上传图片
  chooseImg: function(e) {
    var that = this;
    var imgLength = this.data.images.length;
    wx.chooseImage({
      sizeType: ['original'],
      sourceType: ['album', 'camera'],
      count: 5 - imgLength,
      success: function(res) {
        // 将现有和新上传的合并
        var images = that.data.images.concat(res.tempFilePaths); // 合并原有数组和现传照片
       
        that.setData({
          images: images,
          imgFile: res.tempFilePaths
        })
        that.uploadImages();
      },
    })
  },
  // 上传图片
  uploadImages: function (e) {
    var that = this;
    let { imagesurl, imgFile } = this.data;
    const images = imgFile;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    });
  
    for (var i = 0; i < images.length; i++) {
      var imagesArr = [];
      app.uploadFile({
        url: 'doctor/docExample/uploadFile',
        filePath: images[i],
        success: function (res) {
          var path = JSON.parse(res.data).data[0].filePath;
          
          imagesArr.push(path);
          that.setData({
            imagesurl: imagesurl.concat(imagesArr)
          })
        }
      })
    };
  },
  //  预览图片
  handleImagePreview: function(e) {
    var index = e.target.dataset.index;
    var images = this.data.images;
    wx.previewImage({
      current: images[index],
      urls: images,
    })
  },
  // 删除图片
  handleDeleteImage: function(e) {
    var index = e.target.dataset.index;
    let imagesurl = this.data.imagesurl;
    let images = this.data.images;
    images.splice(index, 1);
    imagesurl.splice(index, 1);
    this.setData({
      images: images,
      imagesurl: imagesurl
    })
  },

  // 完成案例添加
  completeAddCase: function(e) {
    if (!this.data.content) {
      wx.showToast({
        title: '请添加补充信息',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (this.data.images.length == 0) {
      wx.showToast({
        title: '请添加图片',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    this.handleSubmit();
  },

  handleSubmit: function(e) {
    let {
      oneType,
      twoType,
      threeType,
      content,
      goodsId,
      exampleId
    } = this.data;
    var pic = this.data.imagesurl;
    // 创建传值
    const create = {
      oneType,
      twoType,
      threeType,
      goodsId,
      pic,
      content
    }
    // 修改传值
    const updata = {
      oneType,
      twoType,
      threeType,
      goodsId,
      pic,
      content,
      exampleId
    }
    if (exampleId) {
      this.createCase(updata);
    } else {
      this.createCase(create);
    }
  },

  //  修改案例、创建案例
  createCase(param) {
    console.log(param);
    app.request({
      url: 'doctor/docExample/create',
      data: param,
      success(res) {
        wx.redirectTo({
          url: '../doctor-case-add-5/doctor-case-add-5?type=1',
        })
        app.request({
          url: 'doctor/memberTaskApi/addBeauty',
          data: {
            userId: wx.getStorageSync('userid'),
            type: 2
          },
          success(res) {
            console.log('给美丽币给美丽值', res);
          }
        })
      }
    })
  }
})